<!--
 * Copyright © 2022
 * @Author: zhangxiang
 * @Date: 2022-11-21 17:13:52
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-09-21 19:27:49
 * @Description: 
 * 
-->

<template>
  <MyContainer class="container">
    <MyHeader class="header">
      <el-input
        v-model="searchKey"
        style="width: 200px; margin-right: 20px"
        size="medium"
        class="input"
        placeholder="请输入关键字进行过滤"
        clearable
        @clear="clearClick"
        @keyup.enter.native="queryClick"
      />
      <el-button size="medium" type="primary" @click="queryClick"
        >查询</el-button
      >
      <el-button size="medium" @click="resetClick">重置</el-button>
      <el-button
        size="medium"
        style="margin-left: auto"
        type="primary"
        icon="el-icon-plus"
        @click="createClick"
        >创建凭据</el-button
      >
    </MyHeader>
    <MyMain class="main">
      <el-table
        v-loading="loading"
        class="common-table"
        :data="list"
        stripe
        header-cell-class-name="listHeaderCell"
        :header-cell-style="{ color: '#696969' }"
      >
        <el-table-column
          prop="cloudAccountName"
          label="云账号名称"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column prop="cloudProvider" label="云服务商" min-width="120">
        </el-table-column>
        <el-table-column
          prop="cpId"
          label="云商账号ID"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column
          prop="accessKey"
          label="AccessKey"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          prop="desc"
          label="描述"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column prop="createBy" label="创建人" min-width="120" />
        <el-table-column prop="createDate" label="创建时间" width="170" />
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-popover
              :ref="`popover-delete-${scope.$index}`"
              placement="top"
              width="300"
            >
              <p>是否确认删除该凭据？</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="mini"
                  type="text"
                  @click="
                    scope._self.$refs[
                      `popover-delete-${scope.$index}`
                    ].doClose()
                  "
                  >取消</el-button
                >
                <el-button
                  type="primary"
                  size="mini"
                  @click="deleteClick(scope)"
                  >确定</el-button
                >
              </div>
              <el-button
                slot="reference"
                type="danger"
                style="height: 22px; line-height: 22px; padding: 0 10px"
                size="mini"
                >删除</el-button
              >
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" style="margin-top: 30px">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next"
          :page-sizes="[10, 15, 20]"
          :page-size="pageSize"
          :current-page.sync="pageNum"
          :total="totalCount"
          @current-change="changePage"
          @size-change="sizeChange"
        />
      </el-row>
    </MyMain>

    <!--创建-->
    <el-dialog
      v-if="showDialog"
      class="create-dialog"
      :title="'创建公有云凭据'"
      :visible="showDialog"
      width="700px"
      top="15vh"
      :close-on-click-modal="false"
      @close="showDialog = false"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        size="medium"
        label-width="auto"
      >
        <el-form-item label="云服务商" prop="cloudProvider">
          <el-select
            v-model="form.cloudProvider"
            placeholder="请选择云服务商"
            style="width: 100%"
          >
            <el-option
              v-for="item in cloudProviderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="云账号名称" prop="cloudAccountName">
          <el-input
            v-model="form.cloudAccountName"
            placeholder="请输入云账号名称"
          />
        </el-form-item>
        <el-form-item label="云商账号ID" prop="cpId">
          <el-input v-model="form.cpId" placeholder="请输入云商账号ID" />
        </el-form-item>
        <el-form-item label="AccessKeyId" prop="accessKeyId">
          <el-input
            v-model="form.accessKeyId"
            placeholder="请输入AccessKeyId"
          />
        </el-form-item>
        <el-form-item label="SecretAccessKey" prop="secretAccessKey">
          <el-input
            v-model="form.secretAccessKey"
            placeholder="请输入SecretAccessKey"
          />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input
            v-model="form.desc"
            placeholder="请输入描述"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false">取 消</el-button>
        <el-button
          type="primary"
          size="small"
          :loading="dialogConfirmLoading"
          @click="dialogConfirmClick"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </MyContainer>
</template>

<script>
import pagePermissionMixin from "@/views/mixin/pagePermission.js";
import pageQueryMixin from "@/views/mixin/pageQuery.js";
import MyContainer from "@/components/Layout/MyContainer.vue";
import MyHeader from "@/components/Layout/MyHeader.vue";
import MyMain from "@/components/Layout/MyMain.vue";
import {
  getPublicTokenList,
  createPublicToken,
  deletePublicToken,
} from "@/api/cn-mgmt";
import { CLUSTER_PROVIDER } from "@/utils/constant";

// 默认form
const getDefaultForm = () => {
  return {
    cloudProvider: "",
    cloudAccountName: "",
    cpId: "",
    accessKeyId: "",
    secretAccessKey: "",
    desc: "",
  };
};

export default {
  name: "CNMgmtTokenPublic",
  components: {
    MyContainer,
    MyHeader,
    MyMain,
  },
  mixins: [pagePermissionMixin, pageQueryMixin],
  data() {
    return {
      menuKey: "CNToken",
      cloudProviderOptions: CLUSTER_PROVIDER, // 云商选项
      showDialog: false,
      form: getDefaultForm(), // 表单
      // 校验规则
      rules: {
        cloudProvider: [
          { required: true, message: "请选择云服务商", trigger: "change" },
        ],
        cloudAccountName: [
          { required: true, message: "请输入云账户名称", trigger: "blur" },
        ],
        cpId: [
          { required: true, message: "请输入云商账号ID", trigger: "blur" },
        ],
        accessKeyId: [
          { required: true, message: "请输入AccessKeyId", trigger: "blur" },
        ],
        secretAccessKey: [
          { required: true, message: "请输入SecretAccessKey", trigger: "blur" },
        ],
      },
      dialogConfirmLoading: false, // 确定loading
    };
  },
  created() {},
  methods: {
    /**
     * @description: 确定
     * @return {*}
     */
    dialogConfirmClick() {
      // 校验
      this.$refs.form.validate((valid, obj) => {
        if (valid) {
          // 合法
          this.dialogConfirmLoading = true;
          createPublicToken({
            ...this.form,
          })
            .then((res) => {
              this.$message.success({
                message: "创建公有云凭据成功！",
                duration: "2000",
              });
              this.showDialog = false; // 取消弹窗
              this.pageNum = 1; // 第一页
              this.getList(); // 刷新数据
            })
            .finally(() => {
              this.dialogConfirmLoading = false;
            });
        }
      });
    },
    /**
     * @description: 创建
     * @return {*}
     */
    createClick() {
      this.$refs.form && this.$refs.form.resetFields(); // 表单重置
      this.form = getDefaultForm(); // 初始化form
      this.showDialog = true; // 展示
    },
    /**
     * @description: 删除
     * @param {*} row
     * @param {*} index
     * @return {*}
     */
    deleteClick(scope) {
      const row = scope.row;
      deletePublicToken({
        cloudAccountName: row.cloudAccountName,
      }).then((res) => {
        scope._self.$refs[`popover-delete-${scope.$index}`].doClose(); // 取消popover
        this.$message.success("删除成功");
        if (this.list.length === 1) {
          // 当前页只有一条数据时 删除完刷新上一页数据
          this.pageNum = Math.max(this.pageNum - 1, 1);
        }
        this.getList(); // 请求数据
      });
    },
    /**
     * @description: 获取列表
     * @return {*}
     */
    getList() {
      this.handleRequestAndResp(getPublicTokenList, {
        keyword: this.searchKey,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.container {
}
</style>
